package com.itheima.config;
import com.itheima.interceptors.WebsocketInterceptor;
import com.itheima.websocket.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Date :2019-10-26 15:33
 * @Author : yuan_he_song
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private MessageHandler messageHandler;
    @Autowired
    private WebsocketInterceptor websocketInterceptor;


    @Override /** 注册拦截器:Interceptor 和 消息处理类:Handler */
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(messageHandler,"/websocket/{uid}")
                .setAllowedOrigins("*")  /** 允许所有跨域请求 */
                .addInterceptors(websocketInterceptor);
    }
}
